package com.shop.util;

public class EmailVerifyUtil {
	
	
	public boolean stringVerify(String email) {
		if (email.indexOf("@") > 0 && email.indexOf(".") > 0) {
			String prefix = email.substring(0, email.indexOf("@"));
			String middle = email.substring(email.indexOf("@") + 1, email.indexOf("."));
			String subfix = email.substring(email.indexOf(".") + 1);

			if (prefix == null || prefix.length() > 40 || prefix.length() == 0)
				return false;
			if (prefix.length()<6)
				return false;
			if (middle == null || middle.length() > 40 || middle.length() == 0)
				return false;
			if (!isAllWordsAndNo(middle))
				return false;
			if (subfix == null || subfix.length() > 3 || subfix.length() < 2)
				return false;
			if (!isAllWords(subfix))
				return false;
			return true;
		}
		return false;
	}
	
	

	// 检查一个字符串是否全部是字母
	private boolean isAllWords(String prefix) {
		char[] array = prefix.toCharArray();
		for (Character ch : array) {
			if (ch < 'A' || ch > 'z' || (ch < 'a' && ch > 'Z')){
				return false;
			}
		}
		return true;
	}

	// 检查一个字符串是否全部为字母和数字
	private boolean isAllWordsAndNo(String middle) {
		char[] array = middle.toCharArray();
		for (Character ch : array) {
			if (ch < '0' || ch > 'z')
				return false;
			else if (ch > '9' && ch < 'A')
				return false;
			else if (ch > 'Z' && ch < 'a')
				return false;
		}
		return true;
	}
}
